package org.xg.aspect;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;
import org.xg.exception.BusinessServiceException;

@Service
//@Lazy(value=false)
@Aspect
public class AjaxControllerAspect {
private static Log log = LogFactory.getLog(AjaxControllerAspect.class);

//	 @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
//     public void anyRequestMappingMethod() {
//     }
	@Pointcut("@annotation(org.springframework.web.bind.annotation.ResponseBody)")
	public void anyResponseBodyMethod() {}
	
	@Pointcut("execution(*  org.xg.controller..*Controller.*(..))")
	public void anyControllerMethod() {}
	 
//	@Around("anyRequestMappingMethod() && anyResponseBodyMethod() && anyControllerMethod()")

	private final static String MESSAGE = "message";
	private final static String SUCCESS = "success";
	private final static String DATA = "data";
	
	@Around("anyResponseBodyMethod() && anyControllerMethod() ")
	public Object ensulate(ProceedingJoinPoint pjp){
		Map<String,Object> retMap=new HashMap<String,Object>();
		try {
			Object obj=pjp.proceed();
			retMap.put(DATA, obj);
			retMap.put(SUCCESS, true);
			retMap.put(MESSAGE, "操作成功");
		} catch (BusinessServiceException e) {
			retMap.put(SUCCESS, false);
			retMap.put(MESSAGE, e.getMessage());
		} catch (Exception e) {
			retMap.put(SUCCESS, false);
			retMap.put(MESSAGE, "系统错误！");
			log.error("system exception",e);
				e.printStackTrace();
		} catch (Throwable e) {
			retMap.put(SUCCESS, false);
			retMap.put(MESSAGE, "系统错误！");
			log.error("system exception",e);
				e.printStackTrace();
		}
		return retMap;
	}
}
